package com.bookstore.back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bookstore.back.entity.News;
import com.bookstore.back.mapper.NewsMapper;
import com.bookstore.back.service.NewsService;
import com.bookstore.back.service.NewsCategoryRelationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 新闻服务实现类
 * 
 * @author 程序猿_Ti
 * @since 2025-07-19
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {

    @Autowired
    private NewsCategoryRelationService newsCategoryRelationService;

    @Override
    public IPage<News> getNewsList(Map<String, Object> params) {
        // 分页参数
        int current = Integer.parseInt(params.getOrDefault("current", "1").toString());
        int size = Integer.parseInt(params.getOrDefault("size", "10").toString());
        
        // 查询条件
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        
        // 标题搜索
        String title = (String) params.get("title");
        if (StringUtils.isNotBlank(title)) {
            queryWrapper.like("title", title);
        }
        
        // 作者搜索
        String author = (String) params.get("author");
        if (StringUtils.isNotBlank(author)) {
            queryWrapper.like("author", author);
        }
        
        // 状态筛选
        String statusStr = (String) params.get("status");
        if (StringUtils.isNotBlank(statusStr)) {
            Integer status = Integer.parseInt(statusStr);
            queryWrapper.eq("status", status);
        }
        
        // 推荐筛选
        String isRecommendedStr = (String) params.get("isRecommended");
        if (StringUtils.isNotBlank(isRecommendedStr)) {
            Integer isRecommended = Integer.parseInt(isRecommendedStr);
            queryWrapper.eq("is_recommended", isRecommended);
        }
        
        // 按创建时间倒序
        queryWrapper.orderByDesc("created_at");
        
        return this.page(new Page<>(current, size), queryWrapper);
    }

    @Override
    @Transactional
    public boolean createNews(News news) {
        // 设置默认值
        if (news.getStatus() == null) {
            news.setStatus(1); // 默认上线
        }
        if (news.getIsRecommended() == null) {
            news.setIsRecommended(0); // 默认不推荐
        }
        if (news.getViewCount() == null) {
            news.setViewCount(0);
        }
        if (news.getPublishTime() == null) {
            news.setPublishTime(LocalDateTime.now());
        }

        boolean result = this.save(news);

        // 保存分类关系
        if (result && news.getCategoryIds() != null && !news.getCategoryIds().isEmpty()) {
            newsCategoryRelationService.updateNewsCategories(news.getId(), news.getCategoryIds());
        }

        return result;
    }

    @Override
    @Transactional
    public boolean updateNews(Integer newsId, News news) {
        news.setId(newsId);
        boolean result = this.updateById(news);

        // 更新分类关系
        if (result && news.getCategoryIds() != null) {
            newsCategoryRelationService.updateNewsCategories(newsId, news.getCategoryIds());
        }

        return result;
    }

    @Override
    public boolean deleteNews(Integer newsId) {
        return this.removeById(newsId);
    }

    @Override
    public boolean toggleNewsStatus(Integer newsId, Integer status) {
        News news = new News();
        news.setId(newsId);
        news.setStatus(status);
        return this.updateById(news);
    }

    @Override
    public boolean toggleNewsRecommended(Integer newsId, Integer isRecommended) {
        News news = new News();
        news.setId(newsId);
        news.setIsRecommended(isRecommended);
        return this.updateById(news);
    }

    @Override
    public boolean batchDeleteNews(List<Integer> newsIds) {
        return this.removeByIds(newsIds);
    }

}
